package com.example.beaconapk;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;

import com.android.devicehive.DeviceData;

import com.comtrade.client.SampleDeviceClient;
import com.comtrade.ilserver.tasks.BeaconServer;
import com.comtrade.ilserver.tasks.Space;
import com.comtrade.map.BeaconView;
import com.comtrade.map.MapFrame;

public class MonitoringActivity extends Activity {
	MapFrame mp;
	SampleDeviceClient testDevice;
	DeviceData device;
	private ArrayList<BeaconServer> listaBikona;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_monitoring);
		mp=new MapFrame(getApplicationContext());
		Intent i = getIntent();
		byte[] nizbitova=(byte[]) i.getExtras().get("image");
		Bitmap pozadina=BitmapFactory.decodeByteArray(nizbitova,0,nizbitova.length);
		device = (DeviceData) i.getExtras().get("DEVICE");
		testDevice = new SampleDeviceClient(this, device);
		Space prostor= (Space)i.getExtras().get("space");
		int w,h;
		w=Math.abs(prostor.getSpaceCoordinates().get(0).getX()-prostor.getSpaceCoordinates().get(1).getX());
		h=Math.abs(prostor.getSpaceCoordinates().get(0).getY()-prostor.getSpaceCoordinates().get(1).getY());
		mp.getTouchView().setMap(new BitmapDrawable(getResources(), pozadina),w,h);
		Vector<BeaconView> positions= new Vector<BeaconView>();
		listaBikona= prostor.getBeacons();
		for (BeaconServer beaconServer : listaBikona) {
			positions.add(new BeaconView(this,(float)beaconServer.getX(),(float)beaconServer.getY(),beaconServer.getMessage(),mp.getTouchView()));
			Log.d("Bikoni", beaconServer.getX()+" "+beaconServer.getY()+" "+beaconServer.getMessage());
		}
		mp.getTouchView().setBeaconsPositions(positions);
		setContentView(mp);
		try {
			JSONObject data= new JSONObject(device.getData().toString());
			double x = 100*(double) data.get("x");
			double y= 100*(double) data.get("y");
			Log.d("xY", x+" "+y);
			mp.getTouchView().mDot.setCoordinates((float)x,(float) y);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Thread timer = new Thread() {
		    public void run () {
		        for (;;) {
		            // do stuff in a separate thread
		            try {
						Thread.sleep(3000); // TODO Shared Prefs za timeout
						testDevice.reloadDeviceData();
						device= testDevice.getDevice();
						try{
							JSONObject data= new JSONObject(device.getData().toString());
							double x = 100*(double) data.get("x");
							double y= 100*(double) data.get("y");
							Log.d("xY", x+" "+y);
							mp.getTouchView().mDot.setCoordinates((float)x,(float) y);
						}
						catch(JSONException e){
							e.printStackTrace();
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}    // sleep for 3 seconds
		        }
		    }
		};
		timer.start();
	}
}
